package com.zzk.lanouhn.myapplication1.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.WriteArticleActivity;
import com.zzk.lanouhn.myapplication1.adapters.InvestViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/8/23.
 */
public class MessageFragment extends Fragment implements View.OnClickListener {

    private ViewPager vp_message;
    private TabLayout tab_message;
    private List<String> tabNames;
    private List<Fragment> fragments;
    private InvestViewAdapter adapter;
    private ImageView iv_topic, iv_post;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, null);
        vp_message = (ViewPager) view.findViewById(R.id.vp_content_message);
        tab_message = (TabLayout) view.findViewById(R.id.tab_title_message);
        iv_topic = (ImageView) view.findViewById(R.id.iv_topic_icon_message);
        iv_post = (ImageView) view.findViewById(R.id.iv_post_icon_message);
        initData();
        initEvent();
        return view;
    }

    private void initData() {
        tabNames = new ArrayList<>();
        tabNames.add("文章");
        tabNames.add("晒单");

        fragments = new ArrayList<>();
        MessageArticleFragment articleFragment = new MessageArticleFragment();
        fragments.add(articleFragment);
        MessageShowFragment showFragment = new MessageShowFragment();
        fragments.add(showFragment);

        adapter = new InvestViewAdapter(getFragmentManager(), tabNames, fragments);
        vp_message.setAdapter(adapter);
        tab_message.setupWithViewPager(vp_message);
    }

    private void initEvent() {
        tab_message.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        iv_topic.setVisibility(View.VISIBLE);
                        iv_post.setVisibility(View.INVISIBLE);
                        vp_message.setCurrentItem(0);
                        break;
                    case 1:
                        iv_topic.setVisibility(View.INVISIBLE);
                        iv_post.setVisibility(View.VISIBLE);
                        vp_message.setCurrentItem(1);
                        break;
                    default:

                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        iv_post.setOnClickListener(this);
        iv_topic.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_topic_icon_message:
                Intent intent=new Intent(getActivity(), WriteArticleActivity.class);
                startActivity(intent);
                break;
            default:

                break;
        }
    }
}
